var __HbssCountPath = 'http://127.0.0.1:80/thinkstat';

var __Debug=true;//是否debug
var __HbssType;
var __HbssIframe;
var __Guid=__HbssReadCookie('Guid');//唯一ID

var __HbssCountPage = __HbssCountPath + '/Count/count.php';//记录的页面
var __HbssLanguage = (navigator.systemLanguage?navigator.systemLanguage:navigator.language);//语言
var __HbssColor = screen.colorDepth;//色深
var __HbssScreenSize = screen.width + '*' + screen.height;//分辨率
var __HbssCharset = getPageCharset();//编码
var __HbssTimezone = (new Date()).getTimezoneOffset()/60;//时区

var __HbssFirstTime;//第一次访问
var __HbssLastTime;//上一次访问

var __HbssClickUrl=__HbssCountPath+"/Count/count.php";//点击系统的根路径
var __HbssClickPage;//特定的点击分析页面,通用的分析不会记录详细页面
var __HbssReturnPage;//返回作记录的页面,这个需要更进一步扩展
var __HbssReturnType;//返回作记录的类型


/*写入唯一ID*/
if(__Guid==''){
	__Cid=escape(Math.floor(Math.random()*10000)+(new Date()).getTime());
	__HbssWriteCookie('Guid',__Cid,10000);
	__Guid=__HbssReadCookie('Guid');
}

/*是否Iframe*/
if( __HbssIframe == true )
{
	var __HbssPageurl = escape(location.href);
	var __HbssReferer = escape(document.referrer);
}else{
	var __HbssPageurl = escape(top.location.href);
	var __HbssReferer = escape(top.document.referrer);
}

__HbssFirstTime = __HbssReadCookie( '__HbssFirstTime' );
if( __HbssFirstTime == '' )
{
	__HbssFirstTime = getTime();
	__HbssLastTime = __HbssFirstTime;
	__HbssWriteCookie( '__HbssFirstTime', __HbssFirstTime, 10000 );
}
else
{
	__HbssLastTime = getTime();
}

if( __HbssType == null )
{
	__HbssType = 1;
}


/*回访记录*/
__HbssReturnCount = __HbssReadCookie( '__HbssReturnCount' );
__HbssReturnCount = __HbssReturnCount == '' ? 0 : __HbssReturnCount;

__HbssReturnTime = __HbssReadCookie( '__HbssReturnTime' );
if( __HbssReturnTime == '' )
{
	__HbssReturnTime = getTime();
	__HbssWriteCookie( '__HbssReturnTime', __HbssReturnTime, 10000 );
}

temp = __HbssReturnTime.split( '-' )
__HbssReturnTimeDate = new Date(temp[0], temp[1]-1, temp[2], temp[3], temp[4], temp[5] );
__HbssNowTimeDate = new Date();

if( __HbssNowTimeDate - __HbssReturnTimeDate >= 43200000 )
{
	__HbssWriteCookie( '__HbssReturnCount', ++__HbssReturnCount, 10000 );
	__HbssWriteCookie( '__HbssReturnTime', getTime(), 10000 );
}
else
{
	__HbssReturnCount = null;
}


var __HbssCountPathUrl = __HbssCountPage + '?'
+ '&guid=' + __Guid
+ '&actiontype=' + '_stat'
+ '&counturl=' + __HbssCountPath
+ '&pageurl=' + __HbssPageurl
+ '&referer=' + __HbssReferer
+ '&language=' + __HbssLanguage
+ '&color=' + __HbssColor
+ '&screensize=' + __HbssScreenSize
+ '&debug=' + __Debug
+ '&firsttime=' + __HbssFirstTime
+ '&lasttime=' + __HbssLastTime
+ '&type=' + __HbssType
+ '&charset=' + __HbssCharset
+ '&timezone=' + __HbssTimezone;

if( __HbssReturnCount != null )
{
	__HbssCountPathUrl += '&return1=' + __HbssReturnCount;
}


if( __Debug )
{
	document.write(__HbssCountPathUrl);
	document.write("<iframe src='" + __HbssCountPathUrl + "' width='98%' height='350px' ></iframe>");
}
else
{
	document.write("<script src='" + __HbssCountPathUrl + "'></script>");
}



if( __HbssClickPage == null )
{
	__HbssReturnType = 'common';
	__HbssReturnPage = __UrlGetPage( top.location.href,'common' );
}
else
{
	__HbssReturnType = 'click';
	if( __HbssClickPage != top.location.href )
	{
		__HbssReturnPage = null;
	}
	else
	{
		__HbssReturnPage = escape( __UrlGetPage( __HbssClickPage,'' ) );
	}
}


/*通过URL返回一个特定的值*/
function __UrlGetPage(url,rek)
{
	//通过URL返回一个特定的值
	var i=0,h;
	url = url.toLowerCase();
	if ( (i=url.indexOf("://")) < 0 ) return "";//本地文件不处理
	{
		h=url.substring(i+3,url.length);//处理"://"后面的
	}
	if( rek == 'common' )
	{
		if(h.indexOf("?") > 0)
		return h.substring(0,h.indexOf("?"));
		//默认不处理?号后面的包括参数
	}
	return h;
}

if (document.addEventListener)
{
	document.addEventListener("click", __HbssClickEvent, false);
}
else if (document.attachEvent)
{
	document.attachEvent("onclick", __HbssClickEvent);
}

/*处理X,Y的数值*/
function __getScrollXY()
{
	var x = 0;var y = 0;
	if(document.body.scrollTop)
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	else
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	return {x:x,y:y};
}

/*处理点击的数据*/
function __HbssClickEvent(ev)
{
	if( __HbssReturnPage == null ) return '';

	//点击概率 0=100%;1=90%;...9=10%;10=0%;
	if(parseInt(Math.random()* 10) < 0 )
	return;

	ev = ev || window.event;
	var x = ev.clientX + __getScrollXY().x - document.getElementsByTagName("body")[0].offsetLeft;
	var y = ev.clientY + __getScrollXY().y - document.getElementsByTagName("body")[0].offsetTop;
	if (x < 0 || y < 0) return;
	try
	{
		var s = document.createElement("script");
		s.type = "text/javascript";
		s.src = __HbssClickUrl + "?"
		+'&guid=' + __Guid
		+'&actiontype=' + '_click'
		+"&pageid="+__HbssReturnPage
		+"&type="+__HbssReturnType
		+"&w="+screen.width
		+"&x="+x
		+"&y="+y;
		document.getElementsByTagName("head").item(0).appendChild(s);
	}
	catch(e){}
}


/*时间处理函数*/
function getTime()
{
	now = new Date();
	year=now.getYear();
	Month=now.getMonth()+1;
	Day=now.getDate();
	Hour=now.getHours();
	Minute=now.getMinutes();
	Second=now.getSeconds();
	return year+"-"+Month+"-"+Day+"-"+Hour+"-"+Minute+"-"+Second;
}

/*读Cookie*/
function __HbssReadCookie(name)
{
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	//alert(cookieValue);
	return cookieValue;
}

/*写Cookie*/
function __HbssWriteCookie(name, value, hours)
{
	var expire = ";";
	if(hours != null)
	{
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire + "domain=;" + "path=/;";
}

/*获页面编码*/
function getPageCharset(){
	var charSet = "";
	var oType = getBrowser();
	switch(oType){
		case "IE":
		charSet = document.charset;
		break;
		case "FIREFOX":
		charSet = document.characterSet;
		break;
		case "Safari":
		charSet = document.charset;
		break;
		default:
		charSet = document.charset;
		break;
	}
	if(charSet=='x-gbk'){
		charSet = 'gbk';
	}
	return charSet.toLowerCase();
}

/*获浏览器类型*/
function getBrowser(){
	var oType = "";
	if(navigator.userAgent.indexOf("MSIE")!=-1){
		oType="IE";
	}else if(navigator.userAgent.indexOf("Firefox")!=-1){
		oType="FIREFOX";
	}else if(navigator.userAgent.indexOf("Safari")!=-1){
		oType="Safari";//Safari和Chrome都是AppleWebKit
	}
	return oType;
}